package com.MedDroid.entities.schedule;

import com.MedDroid.entities.database.DatabaseField;
import com.MedDroid.entities.database.DatabaseRow;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Romen
 * Date: 19/11/13
 * Time: 9:41 PM
 * Description:
 */
public class ScheduleEvent
{
	int EventID;
	long Date;
	long Duration;
	String Title;
	String Details;
	ArrayList<Integer> Members;

	public ScheduleEvent(long Date, long Duration, String Title, String Details)
	{
		this.Date = Date;
		this.Duration = Duration;
		this.Title = Title;
		this.Details = Details;
	}

	public ScheduleEvent()
	{
		Date = System.currentTimeMillis();
		Duration = 0;
		Title = "";
		Details = "";
	}

	public int getEventID()
	{
		return EventID;
	}

	public long getDate()
	{
		return Date;
	}

	public long getDuration()
	{
		return Duration;
	}

	public String getTitle()
	{
		return Title;
	}

	public String getDetails()
	{
		return Details;
	}

	public void setEventID(int EventID)
	{
		this.EventID = EventID;
	}

	public void setDate(long Time)
	{
		this.Date = Time;
	}

	public void setDuration(long Duration)
	{
		this.Duration = Duration;
	}

	public void setTitle(String title)
	{
		this.Title = title;
	}

	public void setDetails(String details)
	{
		this.Details = details;
	}

	public void addMember(int UserID)
	{
		Members.add(new Integer(UserID));
	}

	public void removeMember(int UserID)
	{
		for (Integer I : Members)
		{
			if (I.intValue() == UserID)
			{
				Members.remove(I);
			}
		}
	}

	public boolean hasMember(int UserID)
	{
		for (Integer I : Members)
		{
			if (I.intValue() == UserID)
			{
				return true;
			}
		}
		return false;
	}

	//CONVERT TO AND FROM ROW FORMAT
	//TODO THIS
	public DatabaseRow buildRow()
	{
		DatabaseRow Row = new DatabaseRow(-1,"Event");
		addField(Row,"EventID",Integer.toString(EventID));
		addField(Row,"Date",Long.toString(Date));
		addField(Row,"Duration",Long.toString(Duration));
		addField(Row,"Title",Title);
		addField(Row,"Details",Details);
		int i=0;
		for (Integer I : Members)
		{
			String label = "Member" + Integer.toString(i);
			addField(Row,label,I.toString());
			i++;
		}
		return new DatabaseRow(-1,"Event");
	}

	private void addField(DatabaseRow Row, String Name, String Value)
	{
		Row.addField(new DatabaseField(Name,Value));
	}

	public static ScheduleEvent buildfromRow(DatabaseRow Row)
	{
		ScheduleEvent NewEvent = new ScheduleEvent();
		NewEvent.EventID = Integer.parseInt(Row.getValue("EventID"));
		NewEvent.Date = Long.parseLong(Row.getValue("Date"));
		NewEvent.Duration = Long.parseLong(Row.getValue("Duration"));
		NewEvent.Title = Row.getValue("Title");
		NewEvent.Details = Row.getValue("Details");
		int i = 0;
		boolean exists = true;
		while(exists)
		{
			String label = "Member" + Integer.toString(i);
			DatabaseField Field = Row.getField(label);
			if (Field != null)
			{
				NewEvent.addMember(Integer.parseInt(Field.getValue()));
			}
			else
			{
				exists = false;
			}
			i++;
		}
		return NewEvent;
	}
}
